<template>
  <div>
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" label-width="68px" size="mini">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-form-item label="保险单号" prop="insuranceOdd">
            <el-input v-model="queryParams.insuranceOdd" clearable placeholder="请输入"
                      @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="queryParams.projectName" clearable placeholder="请输入"
                      @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="保险状态" prop="staffInsuranceStatus">
            <el-select v-model="queryParams.staffInsuranceStatus" clearable placeholder="请选择"
                       @keyup.enter.native="handleQuery">
               <el-option v-for="dict in staffInsuranceStatusOptions" :key="dict.value" :label="dict.label"
            :value="parseInt(dict.value)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="10px">
            <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermi="['cld:insurance:add']" icon="el-icon-plus" plain size="mini" type="primary"
                   @click="handleAdd">新增报案
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="claimsList" border>
      <el-table-column align="center" label="保险单号" prop="insuranceOdd" width="200px;"/>
      <el-table-column align="center" label="保险名称" prop="insuranceName"/>
      <el-table-column align="center" label="提交日期" prop="createTime" width="160px;">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="姓名" prop="staffName"/>
      <el-table-column align="center" label="电话" prop="staffPhone"/>
      <el-table-column align="center" label="岗位编号" prop="projectPostNo"/>
      <el-table-column align="center" label="所属项目" prop="projectName"/>
      <el-table-column align="center" label="描述详情" prop="describe"/>
      <el-table-column align="center" label="照片" prop="imgPics">
        <template slot-scope="scope">
          <image-preview :height="50" :src="scope.row.imgPics" :width="50"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="提交资料" prop="isData">
        <template slot-scope="scope">
          {{ scope.row.isData == 'Y' ? '√' : '否' }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="保险状态" prop="staffInsuranceStatus">
        <template slot-scope="scope">
           <dict-tag :options="staffInsuranceStatusOptions" :value="scope.row.staffInsuranceStatus"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="票据金额" prop="invoiceAmount">
        <template slot-scope="scope">
          {{ toMoney(scope.row.invoiceAmount) }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="理赔金" prop="claimsMoney">
        <template slot-scope="scope">
          {{ toMoney(scope.row.claimsMoney) }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="补助金" prop="subsidesMoney">
        <template slot-scope="scope">
          {{ toMoney(scope.row.subsidesMoney) }}
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button v-if="scope.row.staffInsuranceStatus == 1" v-hasPermi="['cld:insurance:danger']" size="mini" type="text"
                     @click="handleDanger(scope.row)">出险
          </el-button>
          <el-button v-if="scope.row.staffInsuranceStatus == 2" v-hasPermi="['cld:insurance:caseClosed']" size="mini"
                     type="text" @click="handleCaseClosed(scope.row)">结案
          </el-button>
          <el-button v-if="[1, 2].includes(scope.row.staffInsuranceStatus)" v-hasPermi="['cld:insurance:edit']" size="mini"
                     type="text" @click="handleUpdate(scope.row)">编辑
          </el-button>
          <el-button v-if="[1, 2].includes(scope.row.staffInsuranceStatus)" v-hasPermi="['cld:insurance:dismiss']" size="mini" type="text" @click="
            handleDismiss(scope.row)">撤案
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total"
                @pagination="getList"/>

    <!-- 添加或修改保险管理对话框 -->
    <el-dialog :close-on-click-modal="false" :visible.sync="open" append-to-body title="理赔管理" width="800px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" size="mini">
        <el-row v-if="insuranceData.insuranceId" :gutter="20">
          <el-col :span="12">
            <el-form-item label="保险名称" prop="insuranceName">
              <el-input v-model="insuranceData.insuranceName" :disabled="true" placeholder="请输入"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="保险单号" prop="insuranceOdd">
              <el-input v-model="insuranceData.insuranceOdd" :disabled="true" placeholder="请输入"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="!insuranceData.insuranceId" :gutter="20">
          <el-col :span="12">
            <el-form-item label="保险名称" prop="insuranceName">
              <el-select v-model="form.insuranceId" placeholder="请选择" @change="handleInsuranceChange">
                <el-option v-for="item in insuranceOptions" :key="item.insuranceId" :label="item.insuranceName"
                           :value="item.insuranceId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="保险单号" prop="insuranceOdd">
              <el-input v-model="form.insuranceOdd" :disabled="true" placeholder="请输入"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="事故日期" prop="dangerTime">
              <el-date-picker v-model="form.dangerTime" placeholder="选择日期" style="width: 100%;"
                              type="date"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="姓名" prop="staffId">
              <el-input v-model="form.staffName" placeholder="请输入">
                <el-button slot="append" icon="el-icon-search" @click="selectedStaffOpen = true">选择</el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电话" prop="staffPhone">
              <el-input v-model="form.staffPhone" :disabled="true" placeholder="请输入"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="所属项目" prop="projectName">
              <el-input v-model="form.projectName" :disabled="true" placeholder="请输入"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="岗位编号" prop="projectPostNo">
              <el-input v-model="form.projectPostNo" :disabled="true" placeholder="请输入"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="资料提交" prop="isData">
              <el-radio-group v-model="form.isData">
                <el-radio v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.value">{{
                    dict.label
                  }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="票据金额">
              <el-input v-model="form.invoiceAmount" placeholder="请输入" type="number">
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="理赔金">
              <el-input v-model="form.claimsMoney" placeholder="请输入" type="number">
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="补助金">
              <el-input v-model="form.subsidesMoney" placeholder="请输入" type="number">
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="伤情照片" prop="imgPics">
              <image-upload v-model="form.imgPics"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="描述详情" prop="describe">
              <el-input v-model="form.describe" :rows="4" placeholder="请输入" type="textarea"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :close-on-click-modal="false" :destroy-on-close="true" :visible.sync="selectedStaffOpen" append-to-body title="选择员工"
               width="60%">
      <selected-staff v-if="selectedStaffOpen" :params="{ staffStatus: 2, insuranceId: form.insuranceId }"
                      @childInput="handleStaffChange"></selected-staff>
      <div slot="footer" class="dialog-footer">
        <el-button @click="selectedStaffOpen = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addInsuranceDetails,
  delInsuranceDetails,
  getInsuranceDetails,
  getListInsurance,
  listInsuranceDetails,
  updateInsuranceDetails
} from "@/api/cld/insurance";
import selectedStaff from '../staff/selectedStaff.vue';

import {staffInsuranceStatusOptions} from '@/assets/staticDict'

export default {
  name: "claims",
  dicts: ['sys_yes_no'],
  components: {
    selectedStaff
  },
  props: {
    insuranceData: {
      type: Object
    },
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      claimsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 选择人员弹框
      selectedStaffOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        insuranceOdd: this.insuranceData.insuranceOdd,
        insuranceId: this.insuranceData.insuranceId,
        projectName: null,
        insuranceStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        insuranceId: [
          {required: true, message: "请选择保险", trigger: "blur"},
        ],
        staffId: [
          {required: true, message: "请选择员工", trigger: "blur"},
        ],
        describe: [
          {required: true, message: "请输入描述", trigger: "blur"},
        ],
        imgPics: [
          {required: true, message: "请上传伤情照片", trigger: "blur"},
        ],
        isData: [
          {required: true, message: "请选择资料是否上传", trigger: "blur"},
        ],
        dangerTime: [
          {required: true, message: "请选择事故时间", trigger: "blur"},
        ]
      },
      // 保险列表
      insuranceOptions: [],
      // 理赔状态
      staffInsuranceStatusOptions: staffInsuranceStatusOptions
    };
  },
  created() {
    this.getList();
    // 查询保险列表
    this.handleGetListInsurance();
  },
  methods: {
    /** 查询理赔管理列表 */
    getList() {
      this.loading = true;
      listInsuranceDetails(this.queryParams).then(response => {
        this.claimsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 查询保险列表
    handleGetListInsurance() {
      getListInsurance({insuranceStatus: 1}).then(res => {
        this.insuranceOptions = res.data;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        insuranceDetailsId: null,
        insuranceId: this.insuranceData.insuranceId,
        insuranceOdd: this.insuranceData.insuranceOdd,
        staffId: null,
        staffInsuranceStatus: null,
        invoiceAmount: null,
        describe: null,
        isData: 'N',
        claimsMoney: null,
        subsidesMoney: null,
        imgPics: null,
        withdrawReason: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getInsuranceDetails(row.insuranceDetailsId).then(response => {
        this.form = response.data;
        this.open = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.insuranceDetailsId != null) {
            updateInsuranceDetails(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInsuranceDetails(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 撤案
    handleDismiss(row) {
      this.$prompt('', '撤案原因', {
        center: true,
        inputType: 'textarea',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({value}) => {
        let form = {
          insuranceDetailsId: row.insuranceDetailsId,
          withdrawReason: value,
          staffInsuranceStatus: 4
        }
        updateInsuranceDetails(form).then(res => {
          this.getList();
          this.$modal.msgSuccess("操作成功");
        })
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const insuranceDetailsId = row.insuranceDetailsId || this.ids;
      this.$modal.confirm('是否确认删除保险管理编号为"' + insuranceDetailsId + '"的数据项？').then(function () {
        return delInsuranceDetails(insuranceDetailsId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    // 保险选择
    handleInsuranceChange(e) {
      let obj = this.insuranceOptions.find(i => {
        return i.insuranceId == e
      });
      this.form.insuranceOdd = obj.insuranceOdd;
    },
    // 员工选择回调
    handleStaffChange(row) {
      this.selectedStaffOpen = false;
      this.form.staffId = row.staffId;
      this.form.staffName = row.staffName;
      this.form.staffPhone = row.staffPhone;
      this.form.projectName = row.projectName;
      this.form.projectPostNo = row.projectPostNo;
    },
    // 出险
    handleDanger(row) {
      this.$modal.confirm('是否已出险？').then(function () {
        let form = {
          insuranceDetailsId: row.insuranceDetailsId,
          staffInsuranceStatus: 2
        };
        return updateInsuranceDetails(form);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {
      });
    },
    // 结案
    handleCaseClosed(row) {
      this.$modal.confirm('是否已结案？').then(function () {
        let form = {
          insuranceDetailsId: row.insuranceDetailsId,
          staffInsuranceStatus: 3
        };
        return updateInsuranceDetails(form);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {
      });
    }
  }
};
</script>
<style scoped>
.el-range-editor.el-input__inner {
  width: 100%;
}

.el-select {
  width: 100%;
}
</style>
